package com.fh.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.fh.bo.PageFilterBo;
import com.fh.bo.PurRoleBo;
import com.fh.dao.PubDdvMapper;
import com.fh.dao.PurPurview2RoleMapper;
import com.fh.dao.PurRoleMapper;
import com.fh.dao.PurUser2RoleMapper;
import com.fh.dto.PubDdv;
import com.fh.dto.PurPurview2Role;
import com.fh.dto.PurRole;
import com.fh.dto.PurUser2Role;
import com.fh.web.constants.GlobalConstants;
import com.fh.web.service.PurRoleService;
import com.fh.web.util.DateTimeUtils;
import com.fh.web.vo.PurRoleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 角色管理的service接口实现类
 * @author admin
 *
 */
@Service("purRoleService")
public class PurRoleServiceImpl implements PurRoleService{
	// 打印logger日志
	private final Logger LOGGER = LoggerFactory.getLogger(getClass());
		
	@Resource
	private PurRoleMapper purRoleMapper;
	@Resource
	private PurUser2RoleMapper purUser2RoleMapper;
	@Resource
	private PurPurview2RoleMapper purPurview2RoleMapper;
	
	@Resource
	private PubDdvMapper pubDdvMapper;
	
	
	

	@Override
	public Map<String, Object> selectAllRole(PageFilterBo ph) throws Exception {
		PageHelper.startPage(ph.getPage(),ph.getRows());
		List<PurRoleBo> userList = purRoleMapper.selectAllRole(GlobalConstants.PUR_ROLE,GlobalConstants.FILED_NAME);
		PageInfo<PurRoleBo> pageInfo = new PageInfo<PurRoleBo>(userList);
		Map<String, Object> resMap = new HashMap<String, Object>();
		/** 删选数据 */
		for (PurRoleBo roleDto : userList) {
			roleDto.setStrData(DateTimeUtils.formatDateHourMinute(roleDto.getCreateTime()));
		}
		resMap.put("list", userList);
		resMap.put("pageTatal", pageInfo.getTotal());
		return resMap;
	}

	@Override
	public PurRoleVo selectRoleById(Long id) throws Exception {
		PurRole purRole=purRoleMapper.selectByPrimaryKey(Integer.parseInt(id.toString()));
		PurRoleVo vo = new PurRoleVo();
		if(null!=purRole){
			return PurRoleVo.getPurRoleVo(purRole);
		}
		return null;
	}

	@Override
	public void deleteRoleById(Long id) throws Exception {
		/** 先删除关系 */
		Example example = new Example(PurUser2Role.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("roId", id);
		
		purUser2RoleMapper.deleteByExample(example);
		
		Example example2 = new Example(PurPurview2Role.class);
		Criteria criteria2 = example2.createCriteria();
		criteria2.andEqualTo("roId", id);
		purPurview2RoleMapper.deleteByExample(example2);
		/** 删除数据 */
		purRoleMapper.deleteByPrimaryKey(Integer.parseInt(id.toString()));

	}

	@Override
	public void updateRole(PurRole roleDto) throws Exception {
		roleDto.setCreateTime(new Date());
		purRoleMapper.updateByPrimaryKey(roleDto);

	}

	@Override
	public void addRole(PurRoleVo purRoleVo,Integer id) throws Exception {
		purRoleVo.setUserId(id);
		purRoleVo.setCreateTime(new Date());
		purRoleMapper.insert(purRoleVo);
	}

	@Override
	public List<PurRoleVo> selectRoles() throws Exception {
		List<PurRole> list  = purRoleMapper.selectAll();
		List<PurRoleVo> listVo = new ArrayList<PurRoleVo>();
		if(!list.isEmpty()){
			for(PurRole purRole:list){
				listVo.add(PurRoleVo.getPurRoleVo(purRole));
			}
		}
		return listVo;
	}
		
	@Override
	public void deleteUserRoleByUserId(Long id) throws Exception {
		Example example = new Example(PurUser2Role.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userId", id);
		purUser2RoleMapper.deleteByExample(example);
		
	}
	@Override
	public boolean isExistUser(Long id) throws Exception{
		Example example = new Example(PurUser2Role.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("roId", id);
		List<PurUser2Role> list = purUser2RoleMapper.selectByExample(example);
		boolean boo=true;
		if(list.isEmpty()){
			boo=false;
		}
		return boo;
	}
	
	@Override
	public  int checkRole(PurRoleVo purRoleVo){
		Example example2 = new Example(PurRole.class);
		Criteria criteria2 = example2.createCriteria();
		criteria2.andEqualTo("toName", purRoleVo.getToName());
		Integer roId = purRoleVo.getRoId();
		if (null != roId) {
			criteria2.andNotEqualTo("roId", roId);
		}
		List<PurRole> list = purRoleMapper.selectByExample(example2);
		if (!list.isEmpty()) {
			return 2;
		}
		return 0;
  }
	@Override
	public List<PubDdv> getRoleType(){
		try{
			return pubDdvMapper.selectRoleType(GlobalConstants.PUR_ROLE,GlobalConstants.FILED_NAME);
		}catch(Exception e){
			LOGGER.info("查询所有角色类型{}",e);
		}
		return null;
	}

	@Override
	public List<PubDdv> getPurType() {
	  return pubDdvMapper.selectRoleType(GlobalConstants.PUR_PURVIEW,GlobalConstants.PUR_TYPE);
	}
}
